use regex::Regex;
use crate::common::{Loc};


const  MODE_EXTRACT:i32 = 0;
const MODE_REPLACE:i32 = 2;
const  MODE_DEL:i32 = 1;
const MODE_GREEP:i32 =3;
const MODE_GREEP_BY:i32 =4;


pub fn oper_line_function (_:i32, l:&str, re :Option<&Regex>, loc: &Loc){
    let s:&'static str = Box::leak(loc.replace_to.clone().into_boxed_str());
    let ss = match loc.mode{
        MODE_EXTRACT =>{
            Some(re.expect("fuck compile re").find_iter(l).map(|x| x.as_str()).collect::<Vec<&str>>().join(","))
        },
        MODE_DEL =>{
            Some(re.expect("fuck compile re").replace_all(l, "").into_owned())
        },
        MODE_REPLACE =>{
            Some(re.expect("fuck compile re").replace_all(l,s ).into_owned())
        },
        MODE_GREEP => {
            if l.contains(&loc.re_string){
                Some(l.to_owned())
            }else{
                None
            }
        },
        MODE_GREEP_BY => {
            if let Some(k) = loc.grep_by_vec.iter().find(|&compare_line| {
                l.contains(compare_line)
            }){
                // panic!(format!("{} in {}",k,l));
                Some(l.to_owned())
            }else{
                None
            }
        },
        _ => Some(l.to_owned())
    };
    if let Some(new_line) = ss{
        loc.show_line_or_values(crate::common::Value::Single(&new_line));
    };
}

